from PIL import ImageGrab

def test1():
    def get_screen_color(x, y):
        screen = ImageGrab.grab()
        color = screen.getpixel((x, y))
        return color

    x = 100
    y = 100
    color = get_screen_color(x, y)
    print("Color at ({}, {}): {}".format(x, y, color))


import tkinter as tk
import pyautogui
from PIL import ImageGrab

def get_color(event):
    # 获取鼠标位置
    x, y = event.x_root, event.y_root
    # 从屏幕上获取图像
    screen = ImageGrab.grab()
    # 获取对应位置的颜色值
    color = screen.getpixel((x, y))
    # 更新 Label 显示颜色值
    color_label.config(text=f"RGB: {color}", bg=f'#{color[0]:02x}{color[1]:02x}{color[2]:02x}')

# 创建主窗口
root = tk.Tk()
root.title("鼠标颜色提取器")
root.geometry("300x100")

# 创建 Label 显示颜色
color_label = tk.Label(root, text="将鼠标悬停在某处", font=("Arial", 16))
color_label.pack(expand=True, fill=tk.BOTH)

# 绑定鼠标移动事件
root.bind('<Motion>', get_color)

# 运行 Tkinter 主循环
root.mainloop()